package se.webbzon.oschi01.metric;

public class ClassNameMetric implements Metric<String> {
	
	private final Metric<String> stringMetric;
	
	public ClassNameMetric() {
		this(new XWangMetric(true));
	}
	
	public ClassNameMetric(Metric<String> stringMetric) {
		this.stringMetric = stringMetric;
	}

	@Override public double distance(String lhs, String rhs) {
		String[] c1 = lhs.split("\\.");
		String[] c2 = rhs.split("\\.");
		if (c1.length < c2.length) {
			String[] tmp = c1;
			c1 = c2;
			c2 = tmp;
		}
		final double divisor = c1.length * (c1.length + 1) / 2;
		final int k = (int) c1.length;
		double total = 0;
		for (int i = 0; i < c2.length; i++) {
			total += (k - i) * stringMetric.distance(c1[c1.length-i-1], c2[c2.length-i-1]) / divisor; 
		}
		return total;
	}

}
